package api

import (
	"context"
	"github.com/gin-gonic/gin"
	book_rpc "month_zg5/book_srv/proto"
	"month_zg5/client/common/global"
	"month_zg5/client/models"
	"net/http"
	"strconv"
)

func GetDescBook(ctx *gin.Context) {
	res, err := global.BookClient.GetBookDesc(context.Background(), &book_rpc.GetBookDescReq{})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}
	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "login success",
		"data": res.Data,
	})
}

// 高亮搜索  记录搜索记录
func SearchBook(ctx *gin.Context) {
	index := ctx.PostForm("index")
	content := ctx.PostForm("content")
	res, err := global.BookClient.SearchBook(context.Background(), &book_rpc.SearchBookReq{
		Index:   index,
		Content: content,
	})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    200,
		"msg":     "login success",
		"data":    res.Data,
		"keyword": []string{"红楼梦", "海明威", "八维", "上海", "程序员"},
	})
}
func CreateBookshelf(ctx *gin.Context) {
	userid := ctx.PostForm("userid")
	bookid := ctx.PostForm("bookid")
	uid, _ := strconv.Atoi(userid)
	bid, _ := strconv.Atoi(bookid)
	_, err := global.BookClient.BookShelf(context.Background(), &book_rpc.BookShelfReq{
		BookId: int32(bid),
		Userid: int32(uid),
	})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
	})
}
func GetTypeInfo(ctx *gin.Context) {
	res, err := global.BookClient.BookType(context.Background(), &book_rpc.BookTypeReq{})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": res.Data,
	})

}

// 文章详情
func GetDateilInfo(ctx *gin.Context) {
	book_id := ctx.PostForm("book_id")
	id, _ := strconv.Atoi(book_id)
	res, err := global.BookClient.GetBookDetail(context.Background(), &book_rpc.GetBookDetailReq{BookId: int32(id)})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": res.Data,
	})
}

// redis排行榜
func BookTop(ctx *gin.Context) {
	res, err := global.BookClient.BookTop(context.Background(), &book_rpc.BookTopReq{})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": res.Data,
	})
}

// 评论
func CreateCommon(ctx *gin.Context) {
	userid := ctx.PostForm("userid")
	bookid := ctx.PostForm("bookid")
	content := ctx.PostForm("content")
	pid := ctx.PostForm("pid")
	uid, _ := strconv.Atoi(userid)
	bid, _ := strconv.Atoi(bookid)
	id, _ := strconv.Atoi(pid)
	res, err := global.BookClient.CreateCommon(context.Background(), &book_rpc.CreateCommonReq{
		UserId:  int32(uid),
		BookId:  int32(bid),
		Content: content,
		Pid:     int32(id),
	})
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": res.Data,
	})
}

// 历史记录
func ReadList(ctx *gin.Context) {
	userid := ctx.PostForm("userid")
	uid, _ := strconv.Atoi(userid)
	res, err := models.GetReadList(uid)
	if err != nil {
		ctx.JSON(http.StatusUnauthorized, gin.H{
			"code": http.StatusUnauthorized,
			"msg":  err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "success",
		"data": res,
	})
}
